<template>
  <div class="app-container">
    <el-row type="flex" align="center" :gutter="20">
      <el-form class="filterData-block" label-position="left" :inline="true" label-width="auto">
        <el-form-item>
          <el-button size="small" icon="el-icon-plus" type="primary" @click="openDetail(null)">添加培训</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-table :stripe="true" :border="true" :data="tableData">
        <el-table-column align="center" width="50px" prop="id" label="ID"/>
        <el-table-column align="center" min-width="100px" prop="configIdText" label="专业名称"/>
        <el-table-column align="center" min-width="100px" prop="typeIdText" label="所属类别"/>
        <el-table-column align="center" min-width="80px" prop="trainIdText" label="所属学校"/>
        <el-table-column align="center" width="200px" label="操作">
          <template slot-scope="{row}">
            <el-button size="mini" icon="el-icon-edit-outline" type="primary" @click="openDetail(row.id)">编辑
            </el-button>
            <el-button size="mini" icon="el-icon-edit-outline" type="primary" @click="del(row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-dialog width="50%" :append-to-body="true" v-if="visible[0]" :visible.sync="visible[0]" title="添加/编辑" :close-on-click-modal="false"
               :close-on-press-escape="false" :destroy-on-close="true"
    >
      <SpecialityDetail ref="detail" :id="detailId" :trainId="id" :closeDialog="closeDialogSon"></SpecialityDetail>
      <span slot="footer" class="dialog-footer">
        <el-button plain type="primary" @click="visible[0] = false">取 消</el-button>
        <el-button type="primary" class="primary" @click="$refs.detail.submit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import SpecialityDetail from '@/components/TrainCode/SpecialityDetail'
import {delSpecialityByIdApi, editTrainStateApi, getTrainSpecialityListApi} from '@/api/trainCode'
import {deleteLivePortendApi} from "@/api/livePortend";

export default {
  components: { SpecialityDetail },
  name: 'specialityIndex',
  data() {
    return {
      tableData: [],
      visible: {//控制弹窗显示
        0: false
      },
      detailId: null,
      keyword: '',//关键字查询
      filterData: {
        keyword: '',
        state: [],
        time: []
      },
      placeTypeLoading: true,
      trainTypeOption: []
    }
  },
  mounted() {
    if (this.id) {
      this.getTableList()

    }

  },
  props: ['id', 'closeDialog'],
  methods: {
    downloadCode(url) {
      window.open(url)
    },

    updateRowState(row) {
      const { id, state } = row
      editTrainStateApi({ id, state }).then(() => {
        this.$message.success('修改成功')
      }).finally(() => {
        this.getTableList()
      })
    },
    updateState(id,state) {
      editTrainStateApi({ id, state }).then(() => {
        this.$message.success('修改成功')
      }).finally(() => {
        this.getTableList()
      })
    },
    openRecord(sid) {
      //新标签页打开列表
      this.$router.push({
        name: 'TrainCustomer',
        query: {
          sid: sid
        }
      })
    },
    //打开添加/编辑页
    openDetail(id = null) {
      this.detailId = id
      this.visible[0] = true
    },
    //关闭指定弹窗并刷新页面
    closeDialogSon(index) {
      this.visible[index] = false
      this.getTableList()
    },
    del(id){
      this.$confirm('确定删除该专业吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        delSpecialityByIdApi({id}).then((res)=>{
          this.$message.success('删除成功');
          this.getTableList()
        })
      })
    },
    //获取列表
    getTableList() {
      getTrainSpecialityListApi({
        trainId: this.id
      }).then((res) => {
        console.log(res)
        this.tableData = res
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.filterData-block {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
